﻿<%@ WebHandler Language="C#" Class="QueryDomain" %>

using System;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;
using System.Linq;
using System.Xml;

public class QueryDomain : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";


        var ext = context.Request.QueryString["ext"];
        var domain = context.Request.QueryString["domain"];

        var result = Query(ext, domain);

        XmlDocument dom = new XmlDocument();
        dom.LoadXml(result);

        context.Response.Write(dom.DocumentElement.InnerText);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }


    internal string Query(string ext, string domain)
    {
        string a = "";
        string b = "";
        Encoding gb2312 = Encoding.GetEncoding("gb2312");
        if (regzw(domain))
        {
            a = HttpUtility.UrlEncode(domain, System.Text.ASCIIEncoding.GetEncoding("gb2312")).ToString();
        }
        else
        {
            a = domain;
        }
        if (regzw(ext.Replace(".", "")))
        {
            b = HttpUtility.UrlEncode(ext, System.Text.ASCIIEncoding.GetEncoding("gb2312")).ToString();
        }
        else
        {
            b = ext;
        }

        WebRequest req = WebRequest.Create("http://panda.www.net.cn/cgi-bin/check.cgi?area_domain=" + a + b);
        WebResponse res = req.GetResponse();
        Stream ReceiveStream = res.GetResponseStream();
        StreamReader sr = new StreamReader(ReceiveStream);
        string resultstring = sr.ReadToEnd();
        resultstring = HttpUtility.UrlDecode(resultstring, System.Text.ASCIIEncoding.GetEncoding("gb2312")).ToString();
        resultstring = resultstring.Replace("200", "");
        resultstring = resultstring.Replace("300", "");
        resultstring = resultstring.Replace("210", " 域名可以注册！");
        resultstring = resultstring.Replace("211", " 域名已经注册！");
        resultstring = resultstring.Replace("212", " 域名参数传输错误！");
        resultstring = resultstring.Replace("213", " 域名查询超时！");
        resultstring = resultstring.Replace(": Domain name is available", "");
        resultstring = resultstring.Replace(": Domain name is not available", "");
        resultstring = resultstring.Replace(": Domain name is invalid", "");
        resultstring = resultstring.Replace(": Timeout", "");

        return resultstring;
    }

    public static bool regzw(string inputtxt)
    {
        Regex rg = new Regex(@"^[\u4e00-\u9fa5]*\s*$");
        Match mc = rg.Match(inputtxt);
        return mc.Success;
    }
}